package com.china08.yxyapi.api.manage.tec.office.innernotice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.JpushForMessageUtil;
import com.china08.yxyapi.util.JpushForNotificationUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 与 老师 发送 内部通知 发布 按老师发布
 * 
 */
@CrossOrigin
@RestController("TeaInnernoticeCrtController")
public class TeaInnernoticeCrtController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tea/office/innernotice/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        RespModel respModel = new RespModel();
        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String userNick = yxyProfile.getUserNick();
        String avatar = yxyProfile.getUserAvator();
        String schoolNick = yxyProfile.getSchoolName();
        // 将所有添加的老师id 放到一个集合中
        List<YxyNotifictionTea> receiverUser = new ArrayList<YxyNotifictionTea>();
        YxyNotifictionTea yxyNotifictionTea = null;
        List<String> userIdLst = reqModel.receiverId;
        List<String> userNameLst = reqModel.receiverName;
        for (int i = 0; i < userIdLst.size(); i++) {
            yxyNotifictionTea = new YxyNotifictionTea();
            yxyNotifictionTea.setUserId(userIdLst.get(i));
            yxyNotifictionTea.setUserNick(userNameLst.get(i));
            yxyNotifictionTea.setIsDelete(0);
            yxyNotifictionTea.setIsRead(0);
            receiverUser.add(yxyNotifictionTea);
        }

        // ###
        // 创建内部通知
        YxyNoticeInner yxyNoticeInner = new YxyNoticeInner();
        yxyNoticeInner.setUserId(currentUserId);
        yxyNoticeInner.setSchoolId(schoolId);
        yxyNoticeInner.setSchoolNick(schoolNick);
        yxyNoticeInner.setTitle(reqModel.title);
        yxyNoticeInner.setContent(reqModel.content);
        yxyNoticeInner.setReceiver(receiverUser);
        yxyNoticeInner.setAttachmentName(reqModel.attachmentName);
        yxyNoticeInner.setAttachmentPath(reqModel.attachmentPath);
        yxyNoticeInner.setDeleteStatus(0);
        yxyNoticeInner.setImage(reqModel.image);
        yxyNoticeInner.setUserNick(userNick);
        yxyNoticeInner.setAvatar(avatar);
        yxyNoticeInnerRepository.save(yxyNoticeInner);

        // 发送消息
        List<Integer> roleIdList = new ArrayList<>();
        roleIdList.add(0);
        roleIdList.add(2);
        JpushForMessageUtil.sendMessage(1, roleIdList, schoolId, userIdLst, null);

        // 发送通知
        Map<String, String> map = new HashMap<String, String>();
        map.put("modelId", "schoolNotice");
        List<String> tabAndList1 = new ArrayList<>();
        tabAndList1.add("TEACHER" + schoolId);
        List<String> tabAndList2 = new ArrayList<>();
        tabAndList2.add("SCH" + schoolId);

        JpushForNotificationUtil.sendNotification(reqModel.title, "学校通知", map, userIdLst, tabAndList1);
        JpushForNotificationUtil.sendNotification(reqModel.title, "学校通知", map, userIdLst, tabAndList2);

        respModel.setMessage("发通知成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 标题
        public String title;
        // 内容
        public String content;
        // 接受用户id列表
        public List<String> receiverId;
        // 接受用户name列表
        public List<String> receiverName;
        // 接受上传图片
        public List<String> image;
        // 附件原始文件名称
        public String attachmentName;
        // 附件地址
        public String attachmentPath;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(title), "标题不能为空");
            Assert.isTrue(title.length() > 1 && title.length() < 37, "标题要在2-36个文字之间");
            Assert.isTrue(StringUtils.isNotBlank(content), "内容不能为空");
            Assert.isTrue(content.length() > 1 && content.length() < 2501, "内容要在2-2500个文字之间");
            Assert.isTrue(receiverId.size() > 0, "教师不可以为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
